package com.chang.dp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.chang.dp.entity.DictEntity;
import com.chang.dp.enumns.ErrorCodeEnums;
import com.chang.dp.mappers.DictMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@RestController
@RequestMapping("/dict")
@Api(tags = "数据字段")
public class DictController {

    @Autowired
    DictMapper dictMapper;


    @RequestMapping(path = "/create", method = RequestMethod.POST)
    @ApiOperation(value = "新建字段")
    public HashMap<String, Object> create (@RequestBody DictEntity dictEntity) {
        HashMap<String, Object> result = new HashMap<>();
        int rows = dictMapper.insert(dictEntity);
        result.put("code", ErrorCodeEnums.SUCCESS);
        result.put("message", "操作成功");
        result.put("data", dictEntity.getId());
        return result;
    }

    @RequestMapping(path = "/update", method = RequestMethod.POST)
    @ApiOperation(value = "编辑字段")
    public HashMap<String, Object> update (@RequestBody DictEntity dictEntity) {
        HashMap<String, Object> result = new HashMap<>();
        int rows = dictMapper.updateById(dictEntity);
        result.put("code", ErrorCodeEnums.SUCCESS);
        result.put("message", "操作成功");
        return result;
    }

    @RequestMapping(path = "/list", method = RequestMethod.POST)
    @ApiOperation("分页查询")
    public HashMap<String, Object> getListByPage (@RequestBody DictEntity dictEntity) {
        HashMap<String, Object> result = new HashMap<>();
        QueryWrapper<DictEntity> queryWrapper = new QueryWrapper<>(dictEntity);
        PageDTO<DictEntity> dictEntityPageDTO = dictMapper.selectPage(dictEntity.getPagination(), queryWrapper);
        result.put("code", ErrorCodeEnums.SUCCESS);
        result.put("message", "操作成功");
        result.put("data", dictEntityPageDTO);
        return result;
    }

    @RequestMapping(path = "/detail/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "根据id查询")
    public HashMap<String, Object> getGroupDetail (@PathVariable("id") String id) {
        HashMap<String, Object> result = new HashMap<String, Object>();
        DictEntity dictEntity = dictMapper.selectById(id);

        result.put("code", ErrorCodeEnums.SUCCESS);
        result.put("message", "操作成功");
        result.put("data", dictEntity);
        return result;
    }

    @RequestMapping(path = "/delete/{id}", method = RequestMethod.DELETE)
    @ApiOperation(value = "根据id删除")
    public HashMap<String, Object> delete (@PathVariable("id") String id) {
        HashMap<String, Object> result = new HashMap<String, Object>();
        dictMapper.deleteById(id);

        result.put("code", ErrorCodeEnums.SUCCESS);
        result.put("message", "操作成功");
        return result;
    }
}
